﻿<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
  <xsl:param name="indexNum"/>
  <xsl:param name="rootDN" />
    
  <xsl:output method="xml" byte-order-mark="yes" encoding="UTF-8"/>
  
  <xsl:param
    name="scheme_ordering">
    <cn mode="scheme1">SAP_ComputerSystem</cn>
    <cn mode="scheme1">SAP_ApplicationSystem</cn>
    <cn mode="scheme1">SAP_DatabaseInstance</cn>
    <cn mode="scheme1">SAP_DatabaseSystem</cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1">SAP_BCCentralServiceInstance</cn>
    <cn mode="scheme1">SAP_BCClient</cn>
    <cn mode="scheme1">SAP_BCControlInstance</cn>
    <cn mode="scheme1">SAP_BCGateway</cn>
    <cn mode="scheme1">SAP_BCMessageServer</cn>
    <cn mode="scheme1">SAP_BCSystem</cn>
    <cn mode="scheme1">SAP_BusinessSystem</cn>
    <cn mode="scheme1">SAP_BusinessSystemGroup</cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1">SAP_GlobalUniqueID</cn>
    <cn mode="scheme1"></cn>
    <cn mode="scheme1" importance="">SAP_HTTPServicePort</cn>
    <cn mode="scheme1" importance="">SAP_InstalledProduct</cn>
    <cn mode="scheme1" importance="low">SAP_InstalledSoftwareComponent</cn>
    <cn mode="scheme1" importance="low">SAP_InstalledSupportPackage</cn>
    <cn mode="scheme1" importance="high">SAP_IPServicePort</cn>
    <cn mode="scheme1" importance="low">SAP_J2EEDeployedSCService</cn>
    <cn mode="scheme1" importance="low">SAP_J2EEDeployedSCServiceImplementation</cn>
    <cn mode="scheme1" importance="low">SAP_J2EEEngineCluster</cn>
    <cn mode="scheme1" importance="low">SAP_J2EEEngineDispatcherNode</cn>
    <cn mode="scheme1" importance="low">SAP_J2EEEngineInstance</cn>
    <cn mode="scheme1" importance="high">SAP_J2EEEngineServerNode</cn>
    <cn mode="scheme1" importance="low">SAP_JavaDebugPort</cn>
    <cn mode="scheme1" importance="high">SAP_LogicalALESystem</cn>
    <cn mode="scheme1" importance="">SAP_MonitoringSystemGroup</cn>
    <cn mode="scheme1" importance="">SAP_MsgServJCODestination</cn>
    <cn mode="scheme1" importance="high">SAP_StandaloneJavaSystem</cn>
    <cn mode="scheme1" importance="">SAP_SuppliedElements</cn>
    <cn mode="scheme1" importance="low">SAP_SupportPackageType</cn>
    <cn mode="scheme1" importance="high">SAP_SystemLandscapeView</cn>
    <cn mode="scheme1" importance="high">SAP_TCPIPRFCDestination</cn>
    <cn mode="scheme1" importance="high">SAP_XIAdapterFramework</cn>
    <cn mode="scheme1" importance="high">SAP_XIAdapterService</cn>
    <cn mode="scheme1" importance="high">SAP_XIDomain</cn>
    <cn mode="scheme1" importance="high">SAP_XIIntegrationDirectory</cn>
    <cn mode="scheme1" importance="high">SAP_XIIntegrationRepository</cn>
    <cn mode="scheme1" importance="high">SAP_XIIntegrationServer</cn>
    <cn mode="scheme1" importance="">SAP_XIRemoteAdminService</cn>
    <cn mode="scheme1" importance="">SAP_XIRuntimeManagementServer</cn>
    <cn mode="scheme1" importance=""></cn>
    <cn mode="scheme1" importance=""></cn>

    <cn mode="scheme3">SAP_ApplicationSystemHost</cn>
    <cn mode="scheme3">SAP_AppSysInstanceHost</cn>
    <cn mode="scheme3">SAP_AppSysInstanceMember</cn>
    <cn mode="scheme3">SAP_BCApplicationServerDispatcherPort</cn>
    <cn mode="scheme3">SAP_BCApplicationServerGateway</cn>
    <cn mode="scheme3">SAP_BCApplicationServerHost</cn>
    <cn mode="scheme3">SAP_BCCentralServiceEnqueuePort</cn>
    <cn mode="scheme3">SAP_BCCentralServiceInstanceHost</cn>
    <cn mode="scheme3">SAP_BCCentralServiceMessagePort</cn>
    <cn mode="scheme3">SAP_BCCentralServiceMsgHttpPort</cn>
    <cn mode="scheme3">SAP_BCControlInstanceHost</cn>
    <cn mode="scheme3">SAP_BCControlInstanceHTTPServicePort</cn>
    <cn mode="scheme3">SAP_BCMessageServerDispatcherPort</cn>
    <cn mode="scheme3">SAP_BCMessageServerHost</cn>
    <cn mode="scheme3">SAP_BCMessageServerHTTPServicePort</cn>
    <cn mode="scheme3">SAP_BCSystemAgent</cn>
    <cn mode="scheme3">SAP_BCSystemApplicationServer</cn>
    <cn mode="scheme3">SAP_BCSystemCentralApplicationServer</cn>
    <cn mode="scheme3">SAP_BCSystemClient</cn>
    <cn mode="scheme3">SAP_BCSystemJ2EEEngine</cn>
    <cn mode="scheme3">SAP_BCSystemMessageServer</cn>
    <cn mode="scheme3">SAP_BCSystemSystemDB</cn>
    <cn mode="scheme3">SAP_BusinessSystemExchangeServer</cn>
    <cn mode="scheme3">SAP_BusinessSystemGuid</cn>
    <cn mode="scheme3">SAP_BusinessSystemPath</cn>
    <cn mode="scheme3">SAP_BusinessSystemViewedBCClient</cn>
    <cn mode="scheme3" importance="low">SAP_BusinessSystemViewedJ2EEEngineCluster</cn>
    <cn mode="scheme3">SAP_BusinessSystemViewedStandaloneJavaSystem</cn>
    <cn mode="scheme3">SAP_CentrallyMonitoredApplicationSystem</cn>
    <cn mode="scheme3">SAP_CollectedBusinessSystems</cn>
    <cn mode="scheme3">SAP_DBInstanceHost</cn>
    <cn mode="scheme3">SAP_DBSystemInstance</cn>
    <cn mode="scheme3">SAP_ExchangeServerHTTPServicePort</cn>
    <cn mode="scheme3">SAP_HostedJ2EEDeployedSCService</cn>
    <cn mode="scheme3" importance="low">SAP_HostedJCODestination</cn>
    <cn mode="scheme3" importance="">SAP_HostedTCPIPRFCDestination</cn>
    <cn mode="scheme3" importance="">SAP_HostedXIAdapterService</cn>
    <cn mode="scheme3" importance="">SAP_HostedXIRemoteAdminService</cn>
    <cn mode="scheme3" importance="">SAP_InstalledProductImage</cn>
    <cn mode="scheme3" importance="">SAP_InstalledProductLogicalApplicationSystem</cn>
    <cn mode="scheme3" importance="low">SAP_InstalledSupportPackageSoftwareComponent</cn>
    <cn mode="scheme3" importance="low">SAP_InstalledSWComponentOnApplicationSystem</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineClusterInstance</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineClusterMember</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineClusterNodeHost</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineInstanceHost</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineInstanceMember</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineServiceInstance</cn>
    <cn mode="scheme3" importance="low">SAP_J2EEEngineSystemDB</cn>
    <cn mode="scheme3" importance="low">SAP_J2EENodeDebugPort</cn>
    <cn mode="scheme3" importance="low">SAP_J2EENodeServletPort</cn>
    <cn mode="scheme3" importance="low">SAP_LogicalSystemViewedSystem</cn>
    <cn mode="scheme3" importance="low">SAP_SoftwareComponentType</cn>
    <cn mode="scheme3" importance="high">SAP_XIAdapterHostedHTTPServicePort</cn>
    <cn mode="scheme3" importance="low">SAP_XIAdapterServiceAccessByHTTP</cn>
    <cn mode="scheme3" importance="low">SAP_XIContainedAdapter</cn>
    <cn mode="scheme3" importance="low">SAP_XIContainedIntegrationDirectory</cn>
    <cn mode="scheme3" importance="low">SAP_XIContainedIntegrationRepository</cn>
    <cn mode="scheme3" importance="low">SAP_XIContainedIntegrationServer</cn>
    <cn mode="scheme3" importance="low">SAP_XIContainedRuntimeManagementServer</cn>
    <cn mode="scheme3" importance="low">SAP_XIIntegrationServerLogicalIdentity</cn>
    <cn mode="scheme3" importance="low">SAP_XIRemoteAdminServiceAccessByHTTP</cn>
    <cn mode="scheme3" importance="low">SAP_XIViewedXISubSystem</cn>
    <cn mode="scheme3" importance="low"></cn>
    <cn mode="scheme3" importance="low"></cn>
    
    <cn mode="unknown">SAP_CollectedSoftwareComponents</cn>
    <cn mode="unknown">SAP_CollectedSuppliedElements</cn>
    <cn mode="unknown">SAP_ContainedLandscape</cn>
    <cn mode="unknown">SAP_DSRRFCDestinationBCAgent</cn>
    <cn mode="unknown">SAP_HostedAppSysDSRService</cn>
    <cn mode="unknown">SAP_JDISystemLandscape</cn>
    <cn mode="unknown">SAP_LogApplicationSystemInLandscapeView</cn>
    <cn mode="unknown">SAP_RFCDestinationBCAgent</cn>
    <cn mode="unknown" importance="low">SAP_SuppliedElementsSupplier</cn>
    <cn mode="unknown" importance="low">SAP_SuppliedKeyElement</cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
    <cn mode="unknown"></cn>
  </xsl:param>
  
  <xsl:template match="VALUE.NAMEDOBJECT" mode="scheme1">
    <xsl:element name="{INSTANCE/@CLASSNAME}">
      <xsl:for-each select="INSTANCE/QUALIFIER"><xsl:attribute name="{@NAME}"><xsl:value-of
        select="VALUE"/></xsl:attribute></xsl:for-each>
      <xsl:for-each select="INSTANCE/PROPERTY"><xsl:attribute name="{@NAME}"><xsl:value-of
        select="VALUE"/></xsl:attribute></xsl:for-each>
      <xsl:for-each select="INSTANCE/PROPERTY.ARRAY"><array name="{@NAME}">
        <xsl:copy-of select="VALUE.ARRAY/VALUE"/></array></xsl:for-each>
      </xsl:element>
  </xsl:template>  

  <xsl:template match="VALUE.NAMEDOBJECT"  mode="scheme3">
    <xsl:element name="{INSTANCE/@CLASSNAME}">
      <xsl:for-each select="INSTANCE/QUALIFIER"><xsl:attribute name="{@NAME}"><xsl:value-of
        select="VALUE"/></xsl:attribute></xsl:for-each>
      <xsl:for-each select="INSTANCE/PROPERTY"><xsl:attribute name="{@NAME}"><xsl:value-of
      select="VALUE"/></xsl:attribute></xsl:for-each>
      <xsl:for-each select="INSTANCE/PROPERTY.REFERENCE/VALUE.REFERENCE/INSTANCENAME">
        <reference class="{../../@REFERENCECLASS}" category="{../../@NAME}"
          key="{KEYBINDING[@NAME='CreationClassName']/KEYVALUE}" value="{KEYBINDING[@NAME='Name']/KEYVALUE}"/>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>  

  <xsl:template name="recurse">
    <xsl:param name="iter"/>
    <xsl:apply-templates select="document(concat($rootDN,'export', substring('000000000',$iter,1), string($iter),'.xml'))/CIM/DECLARATION/DECLGROUP.WITHNAME"/>
    <xsl:if test="$iter &gt; 1"><xsl:call-template name="recurse"><xsl:with-param name="iter"
      select="number($iter)-1"></xsl:with-param></xsl:call-template></xsl:if>
  </xsl:template>
  
  <xsl:template match="/">
    <hixay:SLD xmlns:hixay="urn:hixay">
    <xsl:if test="$indexNum!=0">
      <xsl:call-template name="recurse"><xsl:with-param name="iter" select="$indexNum"/></xsl:call-template>
    </xsl:if>
    </hixay:SLD>
  </xsl:template>    
  
<xsl:template match="/CIM/DECLARATION/DECLGROUP.WITHNAME">
  <xsl:variable name="src" select="current()"/>

  <xsl:if test="count(//VALUE.NAMEDOBJECT[count(INSTANCENAME)!=1 or count(INSTANCE)!=1]) &gt; 1">
  <xsl:message terminate="no">WARNING: there are unexplored data!</xsl:message>
  </xsl:if>
    <xsl:for-each select="$scheme_ordering/cn[@mode='scheme1']">
      <xsl:comment><xsl:value-of select="current()/text()"></xsl:value-of></xsl:comment>
      <xsl:apply-templates
        select="$src/VALUE.NAMEDOBJECT[INSTANCE/@CLASSNAME=current()]"
        mode="scheme1"
              /></xsl:for-each>
    <xsl:for-each select="$scheme_ordering/cn[@mode='scheme3']">
      <xsl:comment><xsl:value-of select="current()/text()"></xsl:value-of></xsl:comment>
      <xsl:apply-templates
        select="$src/VALUE.NAMEDOBJECT[INSTANCE/@CLASSNAME=current()]"
        mode="scheme3"
      /></xsl:for-each>
    <xsl:for-each select="$scheme_ordering/cn[@mode='unknown']">
      <xsl:comment><xsl:value-of select="current()/text()"></xsl:value-of></xsl:comment>
      <xsl:apply-templates
        select="$src/VALUE.NAMEDOBJECT[INSTANCE/@CLASSNAME=current()]"
        mode="scheme3"
      /></xsl:for-each>
</xsl:template>
</xsl:stylesheet>
